﻿Imports System.Threading
Imports System.Xml
Imports System.ComponentModel
Imports System.Threading.Thread


Public Class replayController
    Public Shared RunID As Integer
    Public Shared QueryID As Integer
    Public Shared ErrorCount As Integer
    Public Shared CompletedQueryCount As Integer

    Public TickCount As Integer
    Private Shared m_NumberofThreads As Integer
    Private Shared Threads() As Thread
    Public startTime As DateTime
    Public EndTime As DateTime
    Public Shared RunLabel As String = ""

    Public Shared Function IncrementError() As Integer
        Interlocked.Increment(ErrorCount)
        Return ErrorCount
    End Function
       


    Public Shared ReadOnly Property NumberOfThreads As Integer
        Get
            Return m_NumberofThreads
        End Get
    End Property

    Public Shared ReadOnly Property QueryCount As Integer
        Get
            Return QueryID - ErrorCount
        End Get
    End Property

    Public Shared Sub IncrementCompletedQueryCount()
        Interlocked.Increment(CompletedQueryCount)
    End Sub

    Public Shared Function IncrementQueryID() As Integer
        Interlocked.Increment(QueryID)
        Return QueryID
    End Function

    Public Shared Function CountQueries() As Integer
        Return SqlHelper.ExcecuteScalar(String.Format("select count(*) from {0}", My.Settings.sqlTable))
    End Function


    Private Function CreateWorkerThread() As Thread
        Dim w As New Worker
        m_NumberofThreads += 1
        Return New Thread(AddressOf w.mdxWorker)

    End Function

    Public Sub CreateWorkers(n As Integer)
        ReDim Threads(My.Settings.Threads - 1)
        For x = 0 To n - 1
            Threads(x) = CreateWorkerThread()
        Next
    End Sub

    Public Sub StartController()
        For Each t In Threads
            t.Start()
        Next
        startTime = Date.Now
        TickCount = Environment.TickCount
    End Sub



    Public Sub StopController()
        TickCount = Environment.TickCount - TickCount
        EndTime = Now


        If RunID > 0 Then
            SqlHelper.ExcecuteNonQuery("usp_runlog_stop", New SqlClient.SqlParameter("RunID", RunID) _
                , New SqlClient.SqlParameter("EndTime", EndTime) _
                , New SqlClient.SqlParameter("QueryCount", QueryID) _
                , New SqlClient.SqlParameter("ErrorCount", ErrorCount) _
                , New SqlClient.SqlParameter("Threads", My.Settings.Threads) _
                , New SqlClient.SqlParameter("TotalTimeSecs", TickCount / 1000) _
                , New SqlClient.SqlParameter("AvgQueryTimeSecs", QueryCount * 1000 / TickCount * My.Settings.Threads))

        End If
    End Sub
    Public Shared Sub StartLog()
        RunLabel = My.Settings.RunLabel
        If RunLabel = "" Then RunLabel = Now.ToString
        If My.Settings.RunID > 0 Then
            RunID = My.Settings.RunID
            QueryID = SqlHelper.ExcecuteScalar(String.Format("select max(QueryID) from QueryLog Where RunID={0}", RunID))
        Else
            RunID = SqlHelper.ExcecuteScalar("usp_runlog_start", New SqlClient.SqlParameter("RunLabel", RunLabel))
        End If

    End Sub

End Class
